class User < ApplicationRecord
	mount_uploader :picture, PictureUploader
	has_many :microposts, dependent: :destroy
	has_many :comments, dependent: :destroy
	has_many :active_relationships, class_name: "Relationship",foreign_key: "follower_id",dependent: :destroy
	has_many :passive_relationships, class_name: "Relationship",foreign_key: "followed_id",dependent: :destroy
	has_many :following, through: :active_relationships, source: :followed
	has_many :followers, through: :passive_relationships, source: :follower
	before_save { self.email = email.downcase }
	validates :name, presence: true, length: { maximum: 50 }
	VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
	validates :email, presence: true, length: { maximum: 255 },
	format: { with: VALID_EMAIL_REGEX },uniqueness: { case_sensitive: false }
	has_secure_password
	validates :password, length: { minimum: 6 }, allow_blank: true


	#random token
	def User.new_token
	SecureRandom.urlsafe_base64
	end

	def feed
	Micropost.where("user_id = ?", id)
	end

	def follow(other_user)
	active_relationships.create(followed_id: other_user.id)
	end
	#
	def unfollow(other_user)
	active_relationships.find_by(followed_id: other_user.id).destroy
	end
	#
	def following?(other_user)
	following.include?(other_user)
	end
end
